#include "stdafx.h"
#include "Engine.h"
#include ".\Object\WowFun.h"
#include "GameStateMgr.h"
#include "GameAnalyze.h"
#include "Factory.h"
#include "ItemBagMgr.h"
#include "MainCtrl.h"
#include "Warning.h"
#include "AntiWarden.h"

CInjectThread* CInjectThread::pThis;
CEngine::CEngine()
{
    m_dwTickJump = 0;
    m_dwTickCount = 0;
    //DWORD dwPID = 0;
    //DWORD dwTID = GetWindowThreadProcessId(CWowFun::WowHwnd(), &dwPID);
    //HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, dwTID);
}

CEngine::~CEngine()
{
}

bool CEngine::Init(CString strConfig)
{
    if ( GET_DB().Init(strConfig) && 
        IFactory::getSingleton().Init() &&
        CGameStateMgr::getSingleton().Init() &&
        CGameAnalyze::getSingleton().Init() )
    {
        return true;
    }
    return false;
}

void CEngine::run()
{

}


bool CEngine::Inject()
{
    m_bState = false;
    //CAntiWarden::getSingleton().HookAddVec(GetCurrentThread());
    return InitInject(this);
}

bool CEngine::Exit()
{
    return Release();
}

void CEngine::stop()
{
    CGameStateMgr::getSingleton().Destory();
    IFactory::getSingleton().Stop();
    CGameAnalyze::getSingleton().SetGoBack(false);
    CItemBagMgr::getSingleton().Stop();
}



DWORD CEngine::ThreadTime(WPARAM wParam, LPARAM lParam)
{
    if (!wParam) return 0;
    if (!lParam) return 0;
    if (m_bState && !CWowFun::WowIsOnLine()) 
    {
        CWarningMgr::Instance().Report(E_OFFLINE);
        CAntiWarden::getSingleton().Remove();
        CAntiWarden::getSingleton().RemoveAdd();
        return 0;
    }
    //CAntiWarden::getSingleton().FunHook(GetCurrentThread());

    PCWPRETSTRUCT pMsg = (PCWPRETSTRUCT) lParam;
    switch (pMsg->wParam)
    {
    case E_MSG_GetPos:
        {
            IFactory::getSingleton().SetPlayerPos();
        }
        break;
    case E_MSG_GetPlayerName:
        {

        }
        break;
    case E_MSG_GetUnitName:
        {
            IFactory::getSingleton().SetUnitName();
        }
        break;
    case E_MSG_Strat:
        {
            m_bState = true;
        }
        break;
    case E_MSG_Stop:
        {
            m_bState = false;
            stop();
        }
        break;
    case E_MSG_GoBackCity:
        {
            //m_bState = true;
            CGameAnalyze::getSingleton().SetGoBack(true);
        }
        break;
    default:
        {
            if (m_bState) Job();
        }
    }
    
    return 0;
}

bool CEngine::Job()
{
    if (!sMyTime::Instance().Go()) 
    {
        return 0;
    }
    if (CWowFun::IsLoading()) 
    {
        IFactory::getSingleton().Stop();
        sMyTime::Instance().Sleep(3000);
        return 0;
    }

    if (!IFactory::getSingleton().GetPlayer())
    {
        sMyTime::Instance().Sleep(1000);
        return 0;
    }
    // game engine
    if (GetTickCount()-m_dwTickCount<100)
    {
        return 0;
    }

    // jump
    if (GetTickCount()-m_dwTickJump>5*60*1000 &&GET_DB().m_bBattle)
    {
        CWowFun::KeyDown(VK_SPACE);
        CWowFun::KeyUp(VK_SPACE);
        m_dwTickJump = GetTickCount();
    }
    m_dwTickCount = GetTickCount();
    IFactory::getSingleton().Run();
    CGameAnalyze::getSingleton().Run();
    CGameStateMgr::getSingleton().Run();
    return 1;
}